layui.define(function(exports){
    exports("xls_format",{
        format_time_string: function(timestamp, formart="Y-m-d H:i:s") {
            //支持的timestamp格式为14位字符串(非time()生成的时间戳)，如20190628142503
            if(timestamp){
                var year = timestamp.substr(0, 4),
                    month = timestamp.substr(4, 2),
                    day = timestamp.substr(6, 2),
                    hour = timestamp.substr(8, 2),
                    min = timestamp.substr(10, 2),
                    sec = timestamp.substr(12, 2);
                return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec;
            }else{
                return "";
            }
        },
        
        format_time: function(date, fmt="YYYY-mm-dd HH:ii:ss"){
            //php时间戳需要转换为js时间戳后才可正确格式化
            if(date.length < 13){
                date = date*1000;
            }
            date = new Date(date);//取得时间戳的Data对象
            let ret;//使用let申明的变量只在作用域内有效
            let opt = {
                "Y+": date.getFullYear().toString(),        // 年
                "m+": (date.getMonth() + 1).toString(),     // 月
                "d+": date.getDate().toString(),            // 日
                "H+": date.getHours().toString(),           // 时
                "i+": date.getMinutes().toString(),         // 分
                "s+": date.getSeconds().toString()          // 秒
                // 有其他格式化字符需求可以继续添加，必须转化成字符串
            };
            for (let k in opt) {
                ret = new RegExp("(" + k + ")").exec(fmt);
                if (ret) {
                    fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
                };
            };
            return fmt;
        },

        format_paytype: function(type){
            if(type == "bankwap"){
                return "银联";
            }
            if(type == "alipay"){
                return "支付宝";
            }
            return type;
        },

        format_amount:function(amount){
            amount = Number(amount);//强制转换为数值型，须传入无符号数值或字符串，否则返回NaN
            amount = amount.toLocaleString('zh-CN', {style: 'currency', currency: 'CNY'});
            return amount;
        },

        format_account_status: function(status){
            if(status & 1){
                return "正常";
            }
            if(status & 2){
                return "已挂失，未补卡";
            }
            if(status & 4){
                return "已挂失，已补卡";
            }
            if(status & 8){
                return "预留";
            }
            return status;
        },

        format_worker_status: function(status){
            var dis;
            switch(status){
                case 0:
                    dis = "正常";
                    break;
                case 1:
                    dis = "已禁用";
                    break;
                default:
                    dis = "错误的状态";
                    break;
            }
            return dis;
        },

        format_worker_role: function(role){
            var nik;
            switch(role){
                case "GM":
                    nik = "系统管理员";
                    break;
                case "FD":
                    nik = "财务人员";
                    break;
                case "OP":
                    nik = "运维人员";
                    break;
                case "VW":
                    nik = "查账人员";
                    break;
                case "HH":
                    nik = "合伙人";
                    break;
                default:
                    nik = "来自火星的你";
                    break;
            }
            return nik;
        },

        format_worker_sex: function(sex){
            var des;
            switch(sex){
                case 0:
                    des = "男";
                    break;
                case 1:
                    des = "女";
                    break;
                default:
                    des = "第三性别";
                    break;
            }
            return des;
        },

        format_worker_pstatus: function(pstatus){
            var des;
            switch(pstatus){
                case 0:
                    des = "初始密码";
                    break;
                case 1:
                    des = "已修改";
                    break;
                default:
                    des = "错误的状态";
                    break;
            }
            return des;
        },

        format_number_thousandth: function(number){
            number = number.toString();// 强制转换为字符串
            reg = /^[\-\+]?((\d+)([\.,](\d{3}))*([\.](\d+))?|([\.,](\d+))?)$/;// 正则
            dh = ",";// 千分符
            dh_reg = new RegExp(dh);
            while (dh_reg.test(number)) {
                number = number.replace(dh_reg, "");// 检测是否包含千分符，如果有，先去除
            }
            if (!reg.test(number)) {
                console.log("您输入的可能不是数字");
                return number;// 如果传值不是数字，则原样返回
            }
            number = number.replace(/^(\d*)$/, "$1.");// 整数加上小数点
            number = (number + "00").replace(/(\d+)(\.\d\d)\d+/, "$1$2");// 
            number = number.replace(".", dh);// 将小数点替换为千分符
            //var re = /(\d)(\d{3},)/;
            var reg_insert = new RegExp("(\\d)(\\d{3}" + dh + ")");// 从小数点(已替换为千分符)前，每三位数插入一个千分符
            while (reg_insert.test(number)) {
                number = number.replace(reg_insert, "$1" + dh + "$2");// 循环插入千分符
            }
            var pointIndex = number.lastIndexOf(dh);// 找到小数点的坐标
            var int = number.substr(0, pointIndex);
            var float = number.substr(pointIndex).replace(dh_reg, ".");
            number = int + float;
            return number;
        }
    })
})